/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.ims.equation.client.gui;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.ims.equation.client.latex.LaTexRenderService;
import com.ims.equation.client.latex.LaTexRenderServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ImageViewer implements EntryPoint {
	private LaTexRenderServiceAsync latexRenderService = LaTexRenderService.Util.getInstance();

	public void addNewValueButtonFor(final RootPanel pPanel, final String pLaTex){
		latexRenderService.base64CodedLatexNotation(pLaTex, 20, new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				final Button newButton = new Button(result);
				newButton.addClickHandler(new ClickHandler(){
					public void onClick(ClickEvent event) {
						Window.alert(pLaTex);
					}
				});
				
				pPanel.add(newButton);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				throw new RuntimeException("Cannot render Latex Notation: " + pLaTex, caught);
				
			}
		});
	}
	
	public ImageViewer(){
		super();
		
		RootPanel rootPanel = RootPanel.get();
		addNewValueButtonFor(rootPanel, "\\frac{n}{d}");
	}
	
	
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		addNewValueButtonFor(rootPanel, "\\frac{n}{d}");
	}
}
